$(document).ready(function() {

    $("form").submit(function () {
        return false;
    });
    var VAL = {
        NORMAL: "0000000",
        EVT_USER: $("#userId").text(),
        T_FBPBISDAT: "FBPBISDAT",
        T_FDPAPLYWO: "FDPAPLYWO",
        T_MIPWOBOM: "MIPWOBOM",
        T_FBPBISMTR: "FBPBISMTR"

    };
    var domObj = {
        $BomInfoDiv: $("#bomInfoListDiv"),
        $BomInfoListGrd: $("#bomInfoListGrd"),
        $WoIDSel: $("#WoIDSel"),
        $MtrlProdidFKSel: $("#MtrlProdidFKSel"),
        $MtrlCateSel: $("#MtrlCateSel"),

        buttons: {
            $query_btn: $("#query_btn"),
        }
    }
    var iniGridInfo = function () {

        var bomInfoCM = [
            {name: 'wo_id_fk', index: 'wo_id_fk', label: WO_ID_TAG, width: '260'},
            {name: 'mtrl_cate', index: 'mtrl_cate', label: MTRL_CATE_TAG, width: '260'},
            {name: 'mtrl_prod_id_fk', index: 'mtrl_prod_id_fk', label: MTRL_ID_TAG, width: '260'},
            {name: 'mtrl_unit', index: 'mtrl_unit', label: MTRL_UNIT_TAG, width: '260'},
            {name: 'mtrl_use_qty', index: 'mtrl_use_qty', label: MTRL_USE_QTY_TAG, width: '260'},
            {name: 'ope_id_fk', index: 'ope_id_fk', label: OPE_ID_TAG, width: '260'},
        ];
        domObj.$BomInfoListGrd.jqGrid({
            url: "",
            datatype: "local",
            mtype: "POST",
            shrinkToFit: true,
            scroll: false,
            autowidth : true, // 宽度根据父元素自适应
            resizable: true,
            rownumbers: true,
            loadonce: true,
            fixed: true,
            viewrecords: true,
            pager: "#bomInfoListPg",
            colModel: bomInfoCM,
            onSelectRow: function (id) {
            }
        });
    }
    var initSelectFnc = {
    	resizeFnc : function() {
	        var offsetBottom, grdDivWidth;
	        grdDivWidth = $("#divTop").width();
	        offsetBottom = $(window).height() - $("#bomInfoListDiv").offset().top;

	        $("#bomInfoListDiv").width(grdDivWidth * 0.99);
	        $("#bomInfoListDiv").height(offsetBottom * 0.99);
	        $("#bomInfoListGrd").setGridWidth(grdDivWidth * 0.99);
	        $("#bomInfoListGrd").setGridHeight(offsetBottom * 0.99 - 101);
	    },
        initWoIDSel: function () {
            var inObj = {
                trx_id: VAL.T_FDPAPLYWO,
                action_flg: "Q",
                iary : []
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                var oaryA = outObj.oary;
                var compare = function (obj1, obj2) {
                    var val1 = obj1.wo_id;
                    var val2 = obj2.wo_id;
                    if (val1 < val2) {
                        return -1;
                    } else if (val1 > val2) {
                        return 1;
                    } else {
                        return 0;
                    }
                }
                oaryA.sort(compare);
                SelectDom.addSelectArr(domObj.$WoIDSel, outObj.oary, "wo_id", "wo_id", "", true);
            }
        },
        initMtrlCateSel: function () {
            var inObj = {
                trx_id : VAL.T_FBPBISDAT,
                action_flg : "Q",
                iary : [{
                    data_cate : "MTCT",
                }]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                var options = [];
                domObj.$MtrlCateSel.append("<option ></option>");
    			for(var i=0; i<outObj.oary.length; i++){
    				if(outObj.oary[i]["data_ext"] != "BACK"){//除去备品备件类型的物料
        				options[i] = "<option value ="+ outObj.oary[i]["data_ext"] +">" + outObj.oary[i]["data_desc"] + "</option>";
    				}
    			}
    			domObj.$MtrlCateSel.append(options.join(""));
                domObj.$MtrlCateSel.select2({
                	theme : "bootstrap"
                });
            }
        },
        initMtrlProdSel: function () {
        	SelectDom.initWithSpace(domObj.$MtrlProdidFKSel);
            var inObj = {
                trx_id: VAL.T_FBPBISMTR,
                action_flg: "Q"
            };

           var mtrlCate = $.trim(domObj.$MtrlCateSel.val());
            if (!mtrlCate) {
                return false;
            } else {
                var iary = [{
                    mtrl_cate : mtrlCate
                }]
                inObj.iary = iary;
            }
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                SelectDom.addSelectArr(domObj.$MtrlProdidFKSel, outObj.oary, "mtrl_prod_id", "mtrl_prod_id", "", true);
            }
        }
    }

    var btnFunc = {
        query_func: function () {
            var inObj, outObj;
            var WoIDSel = $.trim(domObj.$WoIDSel.val());
            if (!WoIDSel) {
                showErrorDialog("", "工单代码不能为空");
                return false;
            }
            inObj = {
                trx_id: VAL.T_MIPWOBOM,
                evt_usr: VAL.EVT_USER,
                action_flg: 'R',
                wo_id_fk: WoIDSel
            };
            var MtrlCateSel = $.trim(domObj.$MtrlCateSel.val());
            if(MtrlCateSel){
                inObj.mtrl_cate = MtrlCateSel;
            }
            var MtrlProdidFKSel = $.trim(domObj.$MtrlProdidFKSel.val());
            if(MtrlProdidFKSel){
               inObj.mtrl_prod_id_fk=MtrlProdidFKSel;
            }
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                setGridInfo(outObj.oaryA, "#bomInfoListGrd");
            }
        }
    }
    
    domObj.buttons.$query_btn.click(function(){
    	btnFunc.query_func();
    });
    
    var initializationFunc = function () {
        iniGridInfo();
        initSelectFnc.resizeFnc();
        initSelectFnc.initWoIDSel();
        initSelectFnc.initMtrlCateSel();
        initSelectFnc.initMtrlProdSel();
    };
    
    domObj.$MtrlCateSel.change(function () {
        initSelectFnc.initMtrlProdSel();
    });
    
    $(window).resize(function () {
    	initSelectFnc.resizeFnc();
    });
    
    initializationFunc();




})





